package com.southpark.system.managers
{
	import com.southpark.system.interfaces.info.IConfirmationBox;
	import com.southpark.system.interfaces.info.ISystemErrorBox;
	import com.southpark.system.interfaces.info.IUserErrorBox;
	import com.southpark.system.interfaces.managers.IApplicationManager;
	import com.southpark.system.interfaces.managers.IInfoManager;
	
	import flash.display.DisplayObject;
	import flash.utils.setTimeout;
	
	import mx.core.IVisualElement;
	
	import spark.components.Group;

	
	public class InfoManager implements IInfoManager
	{
		
		public function InfoManager(applicationManager:IApplicationManager)
		{
			this.applicationManager = applicationManager;
		}
		
		
		private var applicationManager:IApplicationManager;
		
		
		/**
		 * system error
		 */
		private function get systemErrorBox():ISystemErrorBox
		{
			var systemErrorLayer:Group = applicationManager.background.systemErrorLayer;
			var systemErrorBox:ISystemErrorBox = applicationManager.systemErrorBox;
			
			systemErrorLayer.addElement(systemErrorBox as IVisualElement);
			
			return systemErrorBox;
		}
		
		public function showServerError(text:String):void
		{
			if (applicationManager.isDebugMode)
			{
				systemErrorBox.showServerError(text);
			};
		}
		
		public function showApplicationError(text:String):void
		{
			if (applicationManager.isDebugMode)
			{
				systemErrorBox.showApplicationError(text);
			};			
		}
		
		
		/**
		 * user error
		 */
		private var userErrorBox:IUserErrorBox;
		private function createUserErrorBox():Boolean
		{
			if (userErrorBox) return true;
			
			var userErrorBoxClass:Class = applicationManager.assetManager.getAsset(IUserErrorBox);
			if (!userErrorBoxClass) return false;
			
			userErrorBox = new userErrorBoxClass();
			var userErrorLayer:Group = applicationManager.background.userErrorLayer;
			userErrorLayer.addElement(userErrorBox as IVisualElement);
			
			return true;
		}
		
		public function showUserError(text:String,removeAfterDelay:Number=0):void
		{
			if (!createUserErrorBox()) return;
			userErrorBox.showUserError(text,removeAfterDelay);
		}
		
		public function hidePreviousUserErrors():void
		{
			if (userErrorBox) userErrorBox.hideUserError();
		}
		
		
		
		
		/**
		 * confirmation
		 */
		public function showConfirmation(text:String,removeAfterDelay:Number=0):void
		{
			hidePreviousUserErrors();
			
			var confirmationLayer:Group = applicationManager.background.confirmationLayer;
			
			var confirmationBoxClass:Class = applicationManager.assetManager.getAsset(IConfirmationBox);
			if (!confirmationBoxClass) return;
			var confirmationBox:IConfirmationBox = new confirmationBoxClass();
			
			confirmationBox.showConfirmation(text);
			confirmationLayer.addElement(confirmationBox as IVisualElement);
			
			if (removeAfterDelay != 0)
			{
				setTimeout(function():void
				{
					if (confirmationLayer.mxmlContent.indexOf(confirmationBox) != -1)
					{	
						applicationManager.background.confirmationLayer.removeElement(confirmationBox as IVisualElement);
					};
				},removeAfterDelay);
			};
		}
		
		
		
		
		
	}
}